{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "impressed-accessory",
   "metadata": {},
   "source": [
    "## Seldon V2 Kubernetes Examples\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "91687b33",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"NAMESPACE\"] = \"seldon-mesh\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "sunrise-commercial",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'172.18.255.2'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MESH_IP=!kubectl get svc seldon-mesh -n ${NAMESPACE} -o jsonpath='{.status.loadBalancer.ingress[0].ip}'\n",
    "MESH_IP=MESH_IP[0]\n",
    "import os\n",
    "os.environ['MESH_IP'] = MESH_IP\n",
    "MESH_IP"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "speaking-bracelet",
   "metadata": {},
   "source": [
    "### Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "operating-console",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: iris\r\n",
      "spec:\r\n",
      "  storageUri: \"gs://seldon-models/scv2/samples/mlserver_1.3.5/iris-sklearn\"\r\n",
      "  requirements:\r\n",
      "  - sklearn\r\n",
      "  memory: 100Ki\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/sklearn-iris-gs.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "exempt-bumper",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/iris created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f ./models/sklearn-iris-gs.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "beneficial-logan",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/iris condition met\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s model --all -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "prepared-duration",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"conditions\": [\r\n",
      "    {\r\n",
      "      \"lastTransitionTime\": \"2023-06-30T10:01:52Z\",\r\n",
      "      \"message\": \"ModelAvailable\",\r\n",
      "      \"status\": \"True\",\r\n",
      "      \"type\": \"ModelReady\"\r\n",
      "    },\r\n",
      "    {\r\n",
      "      \"lastTransitionTime\": \"2023-06-30T10:01:52Z\",\r\n",
      "      \"status\": \"True\",\r\n",
      "      \"type\": \"Ready\"\r\n",
      "    }\r\n",
      "  ],\r\n",
      "  \"replicas\": 1\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl get model iris -n ${NAMESPACE} -o jsonpath='{.status}' | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "67900afd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "\t\"model_name\": \"iris_1\",\r\n",
      "\t\"model_version\": \"1\",\r\n",
      "\t\"id\": \"7fd401e1-3dce-46f5-9668-902aea652b89\",\r\n",
      "\t\"parameters\": {},\r\n",
      "\t\"outputs\": [\r\n",
      "\t\t{\r\n",
      "\t\t\t\"name\": \"predict\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t1,\r\n",
      "\t\t\t\t1\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"datatype\": \"INT64\",\r\n",
      "\t\t\t\"parameters\": {\r\n",
      "\t\t\t\t\"content_type\": \"np\"\r\n",
      "\t\t\t},\r\n",
      "\t\t\t\"data\": [\r\n",
      "\t\t\t\t2\r\n",
      "\t\t\t]\r\n",
      "\t\t}\r\n",
      "\t]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris --inference-host ${MESH_IP}:80 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a1848c7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"modelName\": \"iris_1\",\r\n",
      "  \"modelVersion\": \"1\",\r\n",
      "  \"outputs\": [\r\n",
      "    {\r\n",
      "      \"name\": \"predict\",\r\n",
      "      \"datatype\": \"INT64\",\r\n",
      "      \"shape\": [\r\n",
      "        \"1\",\r\n",
      "        \"1\"\r\n",
      "      ],\r\n",
      "      \"parameters\": {\r\n",
      "        \"content_type\": {\r\n",
      "          \"stringParam\": \"np\"\r\n",
      "        }\r\n",
      "      },\r\n",
      "      \"contents\": {\r\n",
      "        \"int64Contents\": [\r\n",
      "          \"2\"\r\n",
      "        ]\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris --inference-mode grpc --inference-host ${MESH_IP}:80 \\\n",
    "   '{\"model_name\":\"iris\",\"inputs\":[{\"name\":\"input\",\"contents\":{\"fp32_contents\":[1,2,3,4]},\"datatype\":\"FP32\",\"shape\":[1,4]}]}' | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "attended-vanilla",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"conditions\": [\r\n",
      "    {\r\n",
      "      \"lastTransitionTime\": \"2023-06-30T09:59:12Z\",\r\n",
      "      \"status\": \"True\",\r\n",
      "      \"type\": \"Ready\"\r\n",
      "    },\r\n",
      "    {\r\n",
      "      \"lastTransitionTime\": \"2023-06-30T09:59:12Z\",\r\n",
      "      \"reason\": \"StatefulSet replicas matches desired replicas\",\r\n",
      "      \"status\": \"True\",\r\n",
      "      \"type\": \"StatefulSetReady\"\r\n",
      "    }\r\n",
      "  ],\r\n",
      "  \"loadedModels\": 1,\r\n",
      "  \"replicas\": 1,\r\n",
      "  \"selector\": \"seldon-server-name=mlserver\"\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl get server mlserver -n ${NAMESPACE} -o jsonpath='{.status}' | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "precious-development",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io \"iris\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f ./models/sklearn-iris-gs.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lined-inspector",
   "metadata": {},
   "source": [
    "### Experiment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "daily-stretch",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: iris\r\n",
      "spec:\r\n",
      "  storageUri: \"gs://seldon-models/mlserver/iris\"\r\n",
      "  requirements:\r\n",
      "  - sklearn\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/sklearn1.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "experienced-egypt",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: iris2\r\n",
      "spec:\r\n",
      "  storageUri: \"gs://seldon-models/mlserver/iris\"\r\n",
      "  requirements:\r\n",
      "  - sklearn\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/sklearn2.yaml "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "utility-sweden",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/iris created\n",
      "model.mlops.seldon.io/iris2 created\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f ./models/sklearn1.yaml -n ${NAMESPACE}\n",
    "!kubectl create -f ./models/sklearn2.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "african-blair",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/iris condition met\n",
      "model.mlops.seldon.io/iris2 condition met\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s model --all -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "trained-steering",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Experiment\r\n",
      "metadata:\r\n",
      "  name: experiment-sample\r\n",
      "spec:\r\n",
      "  default: iris\r\n",
      "  candidates:\r\n",
      "  - name: iris\r\n",
      "    weight: 50\r\n",
      "  - name: iris2\r\n",
      "    weight: 50\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./experiments/ab-default-model.yaml "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "saving-links",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "experiment.mlops.seldon.io/experiment-sample created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f ./experiments/ab-default-model.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "finnish-rider",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "experiment.mlops.seldon.io/experiment-sample condition met\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s experiment --all -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "effective-athens",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris2_1::29 :iris_1::21]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer --inference-host ${MESH_IP}:80 -i 50 iris \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "widespread-middle",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "experiment.mlops.seldon.io \"experiment-sample\" deleted\n",
      "model.mlops.seldon.io \"iris\" deleted\n",
      "model.mlops.seldon.io \"iris2\" deleted\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f ./experiments/ab-default-model.yaml -n ${NAMESPACE}\n",
    "!kubectl delete -f ./models/sklearn1.yaml -n ${NAMESPACE}\n",
    "!kubectl delete -f ./models/sklearn2.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4712c36",
   "metadata": {},
   "source": [
    "### Pipeline - model chain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1b1f0a00",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\n",
      "kind: Model\n",
      "metadata:\n",
      "  name: tfsimple1\n",
      "spec:\n",
      "  storageUri: \"gs://seldon-models/triton/simple\"\n",
      "  requirements:\n",
      "  - tensorflow\n",
      "  memory: 100Ki\n",
      "apiVersion: mlops.seldon.io/v1alpha1\n",
      "kind: Model\n",
      "metadata:\n",
      "  name: tfsimple2\n",
      "spec:\n",
      "  storageUri: \"gs://seldon-models/triton/simple\"\n",
      "  requirements:\n",
      "  - tensorflow\n",
      "  memory: 100Ki\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/tfsimple1.yaml \n",
    "!cat ./models/tfsimple2.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "776122d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/tfsimple1 created\n",
      "model.mlops.seldon.io/tfsimple2 created\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f ./models/tfsimple1.yaml -n ${NAMESPACE}\n",
    "!kubectl create -f ./models/tfsimple2.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2499e435",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/tfsimple1 condition met\n",
      "model.mlops.seldon.io/tfsimple2 condition met\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s model --all -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "70c78cee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Pipeline\r\n",
      "metadata:\r\n",
      "  name: tfsimples\r\n",
      "spec:\r\n",
      "  steps:\r\n",
      "    - name: tfsimple1\r\n",
      "    - name: tfsimple2\r\n",
      "      inputs:\r\n",
      "      - tfsimple1\r\n",
      "      tensorMap:\r\n",
      "        tfsimple1.outputs.OUTPUT0: INPUT0\r\n",
      "        tfsimple1.outputs.OUTPUT1: INPUT1\r\n",
      "  output:\r\n",
      "    steps:\r\n",
      "    - tfsimple2\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./pipelines/tfsimples.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "69033959",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pipeline.mlops.seldon.io/tfsimples created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f ./pipelines/tfsimples.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1d6f2f47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pipeline.mlops.seldon.io/tfsimples condition met\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s pipeline --all -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "6fb5a35c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"outputs\": [\r\n",
      "    {\r\n",
      "      \"name\": \"OUTPUT0\",\r\n",
      "      \"datatype\": \"INT32\",\r\n",
      "      \"shape\": [\r\n",
      "        \"1\",\r\n",
      "        \"16\"\r\n",
      "      ],\r\n",
      "      \"contents\": {\r\n",
      "        \"intContents\": [\r\n",
      "          2,\r\n",
      "          4,\r\n",
      "          6,\r\n",
      "          8,\r\n",
      "          10,\r\n",
      "          12,\r\n",
      "          14,\r\n",
      "          16,\r\n",
      "          18,\r\n",
      "          20,\r\n",
      "          22,\r\n",
      "          24,\r\n",
      "          26,\r\n",
      "          28,\r\n",
      "          30,\r\n",
      "          32\r\n",
      "        ]\r\n",
      "      }\r\n",
      "    },\r\n",
      "    {\r\n",
      "      \"name\": \"OUTPUT1\",\r\n",
      "      \"datatype\": \"INT32\",\r\n",
      "      \"shape\": [\r\n",
      "        \"1\",\r\n",
      "        \"16\"\r\n",
      "      ],\r\n",
      "      \"contents\": {\r\n",
      "        \"intContents\": [\r\n",
      "          2,\r\n",
      "          4,\r\n",
      "          6,\r\n",
      "          8,\r\n",
      "          10,\r\n",
      "          12,\r\n",
      "          14,\r\n",
      "          16,\r\n",
      "          18,\r\n",
      "          20,\r\n",
      "          22,\r\n",
      "          24,\r\n",
      "          26,\r\n",
      "          28,\r\n",
      "          30,\r\n",
      "          32\r\n",
      "        ]\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline infer tfsimples --inference-mode grpc --inference-host ${MESH_IP}:80 \\\n",
    "    '{\"model_name\":\"simple\",\"inputs\":[{\"name\":\"INPUT0\",\"contents\":{\"int_contents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]},{\"name\":\"INPUT1\",\"contents\":{\"int_contents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]}]}' | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "2487f322",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pipeline.mlops.seldon.io \"tfsimples\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f ./pipelines/tfsimples.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "58f3b20c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io \"tfsimple1\" deleted\n",
      "model.mlops.seldon.io \"tfsimple2\" deleted\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f ./models/tfsimple1.yaml -n ${NAMESPACE}\n",
    "!kubectl delete -f ./models/tfsimple2.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92b3ea6b",
   "metadata": {},
   "source": [
    "### Pipeline - model join"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f3c791fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\n",
      "kind: Model\n",
      "metadata:\n",
      "  name: tfsimple1\n",
      "spec:\n",
      "  storageUri: \"gs://seldon-models/triton/simple\"\n",
      "  requirements:\n",
      "  - tensorflow\n",
      "  memory: 100Ki\n",
      "apiVersion: mlops.seldon.io/v1alpha1\n",
      "kind: Model\n",
      "metadata:\n",
      "  name: tfsimple2\n",
      "spec:\n",
      "  storageUri: \"gs://seldon-models/triton/simple\"\n",
      "  requirements:\n",
      "  - tensorflow\n",
      "  memory: 100Ki\n",
      "apiVersion: mlops.seldon.io/v1alpha1\n",
      "kind: Model\n",
      "metadata:\n",
      "  name: tfsimple3\n",
      "spec:\n",
      "  storageUri: \"gs://seldon-models/triton/simple\"\n",
      "  requirements:\n",
      "  - tensorflow\n",
      "  memory: 100Ki\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/tfsimple1.yaml\n",
    "!cat ./models/tfsimple2.yaml\n",
    "!cat ./models/tfsimple3.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "779ebd6b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/tfsimple1 created\n",
      "model.mlops.seldon.io/tfsimple2 created\n",
      "model.mlops.seldon.io/tfsimple3 created\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f ./models/tfsimple1.yaml -n ${NAMESPACE}\n",
    "!kubectl create -f ./models/tfsimple2.yaml -n ${NAMESPACE}\n",
    "!kubectl create -f ./models/tfsimple3.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "79a7088d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/tfsimple1 condition met\n",
      "model.mlops.seldon.io/tfsimple2 condition met\n",
      "model.mlops.seldon.io/tfsimple3 condition met\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s model --all -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c6a0e53e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Pipeline\r\n",
      "metadata:\r\n",
      "  name: join\r\n",
      "spec:\r\n",
      "  steps:\r\n",
      "    - name: tfsimple1\r\n",
      "    - name: tfsimple2\r\n",
      "    - name: tfsimple3      \r\n",
      "      inputs:\r\n",
      "      - tfsimple1.outputs.OUTPUT0\r\n",
      "      - tfsimple2.outputs.OUTPUT1\r\n",
      "      tensorMap:\r\n",
      "        tfsimple1.outputs.OUTPUT0: INPUT0\r\n",
      "        tfsimple2.outputs.OUTPUT1: INPUT1\r\n",
      "  output:\r\n",
      "    steps:\r\n",
      "    - tfsimple3\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./pipelines/tfsimples-join.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "1ff21deb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pipeline.mlops.seldon.io/join created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f ./pipelines/tfsimples-join.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "9984ebf6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pipeline.mlops.seldon.io/join condition met\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s pipeline --all -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "06c0f069",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"outputs\": [\r\n",
      "    {\r\n",
      "      \"name\": \"OUTPUT0\",\r\n",
      "      \"datatype\": \"INT32\",\r\n",
      "      \"shape\": [\r\n",
      "        \"1\",\r\n",
      "        \"16\"\r\n",
      "      ],\r\n",
      "      \"contents\": {\r\n",
      "        \"intContents\": [\r\n",
      "          2,\r\n",
      "          4,\r\n",
      "          6,\r\n",
      "          8,\r\n",
      "          10,\r\n",
      "          12,\r\n",
      "          14,\r\n",
      "          16,\r\n",
      "          18,\r\n",
      "          20,\r\n",
      "          22,\r\n",
      "          24,\r\n",
      "          26,\r\n",
      "          28,\r\n",
      "          30,\r\n",
      "          32\r\n",
      "        ]\r\n",
      "      }\r\n",
      "    },\r\n",
      "    {\r\n",
      "      \"name\": \"OUTPUT1\",\r\n",
      "      \"datatype\": \"INT32\",\r\n",
      "      \"shape\": [\r\n",
      "        \"1\",\r\n",
      "        \"16\"\r\n",
      "      ],\r\n",
      "      \"contents\": {\r\n",
      "        \"intContents\": [\r\n",
      "          2,\r\n",
      "          4,\r\n",
      "          6,\r\n",
      "          8,\r\n",
      "          10,\r\n",
      "          12,\r\n",
      "          14,\r\n",
      "          16,\r\n",
      "          18,\r\n",
      "          20,\r\n",
      "          22,\r\n",
      "          24,\r\n",
      "          26,\r\n",
      "          28,\r\n",
      "          30,\r\n",
      "          32\r\n",
      "        ]\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline infer join --inference-mode grpc --inference-host ${MESH_IP}:80 \\\n",
    "    '{\"model_name\":\"simple\",\"inputs\":[{\"name\":\"INPUT0\",\"contents\":{\"int_contents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]},{\"name\":\"INPUT1\",\"contents\":{\"int_contents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]}]}' | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "cd63243f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pipeline.mlops.seldon.io \"join\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f ./pipelines/tfsimples-join.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "a825bfe6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io \"tfsimple1\" deleted\n",
      "model.mlops.seldon.io \"tfsimple2\" deleted\n",
      "model.mlops.seldon.io \"tfsimple3\" deleted\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f ./models/tfsimple1.yaml -n ${NAMESPACE}\n",
    "!kubectl delete -f ./models/tfsimple2.yaml -n ${NAMESPACE}\n",
    "!kubectl delete -f ./models/tfsimple3.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31f99eda",
   "metadata": {},
   "source": [
    "## Explainer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "2c21f9c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: income\r\n",
      "spec:\r\n",
      "  storageUri: \"gs://seldon-models/scv2/examples/mlserver_1.3.5/income/classifier\"\r\n",
      "  requirements:\r\n",
      "  - sklearn\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/income.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "505d06ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/income created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f ./models/income.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "46f83c79",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/income condition met\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s model --all -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "00f5c247",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"conditions\": [\r\n",
      "    {\r\n",
      "      \"lastTransitionTime\": \"2023-06-30T10:02:53Z\",\r\n",
      "      \"message\": \"ModelAvailable\",\r\n",
      "      \"status\": \"True\",\r\n",
      "      \"type\": \"ModelReady\"\r\n",
      "    },\r\n",
      "    {\r\n",
      "      \"lastTransitionTime\": \"2023-06-30T10:02:53Z\",\r\n",
      "      \"status\": \"True\",\r\n",
      "      \"type\": \"Ready\"\r\n",
      "    }\r\n",
      "  ],\r\n",
      "  \"replicas\": 1\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl get model income -n ${NAMESPACE} -o jsonpath='{.status}' | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "02d28f57",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "\t\"model_name\": \"income_1\",\r\n",
      "\t\"model_version\": \"1\",\r\n",
      "\t\"id\": \"f52acfeb-0f22-429f-8c7a-785ef17cd470\",\r\n",
      "\t\"parameters\": {},\r\n",
      "\t\"outputs\": [\r\n",
      "\t\t{\r\n",
      "\t\t\t\"name\": \"predict\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t1,\r\n",
      "\t\t\t\t1\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"datatype\": \"INT64\",\r\n",
      "\t\t\t\"parameters\": {\r\n",
      "\t\t\t\t\"content_type\": \"np\"\r\n",
      "\t\t\t},\r\n",
      "\t\t\t\"data\": [\r\n",
      "\t\t\t\t0\r\n",
      "\t\t\t]\r\n",
      "\t\t}\r\n",
      "\t]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer income --inference-host ${MESH_IP}:80 \\\n",
    "     '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 12], \"datatype\": \"FP32\", \"data\": [[47,4,1,1,1,3,4,1,0,0,40,9]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "52ec12eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: income-explainer\r\n",
      "spec:\r\n",
      "  storageUri: \"gs://seldon-models/scv2/examples/mlserver_1.3.5/income/explainer\"\r\n",
      "  explainer:\r\n",
      "    type: anchor_tabular\r\n",
      "    modelRef: income\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/income-explainer.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "2e028f78",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/income-explainer created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f ./models/income-explainer.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "8b8035b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/income condition met\n",
      "model.mlops.seldon.io/income-explainer condition met\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s model --all -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "67489d73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"conditions\": [\r\n",
      "    {\r\n",
      "      \"lastTransitionTime\": \"2023-06-30T10:03:07Z\",\r\n",
      "      \"message\": \"ModelAvailable\",\r\n",
      "      \"status\": \"True\",\r\n",
      "      \"type\": \"ModelReady\"\r\n",
      "    },\r\n",
      "    {\r\n",
      "      \"lastTransitionTime\": \"2023-06-30T10:03:07Z\",\r\n",
      "      \"status\": \"True\",\r\n",
      "      \"type\": \"Ready\"\r\n",
      "    }\r\n",
      "  ],\r\n",
      "  \"replicas\": 1\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl get model income-explainer -n ${NAMESPACE} -o jsonpath='{.status}' | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "4f6cbcac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "\t\"model_name\": \"income-explainer_1\",\r\n",
      "\t\"model_version\": \"1\",\r\n",
      "\t\"id\": \"3028a904-9bb3-42d7-bdb7-6e6993323ed7\",\r\n",
      "\t\"parameters\": {},\r\n",
      "\t\"outputs\": [\r\n",
      "\t\t{\r\n",
      "\t\t\t\"name\": \"explanation\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t1,\r\n",
      "\t\t\t\t1\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"datatype\": \"BYTES\",\r\n",
      "\t\t\t\"parameters\": {\r\n",
      "\t\t\t\t\"content_type\": \"str\"\r\n",
      "\t\t\t},\r\n",
      "\t\t\t\"data\": [\r\n",
      "\t\t\t\t\"{\\\"meta\\\": {\\\"name\\\": \\\"AnchorTabular\\\", \\\"type\\\": [\\\"blackbox\\\"], \\\"explanations\\\": [\\\"local\\\"], \\\"params\\\": {\\\"seed\\\": 1, \\\"disc_perc\\\": [25, 50, 75], \\\"threshold\\\": 0.95, \\\"delta\\\": 0.1, \\\"tau\\\": 0.15, \\\"batch_size\\\": 100, \\\"coverage_samples\\\": 10000, \\\"beam_size\\\": 1, \\\"stop_on_first\\\": false, \\\"max_anchor_size\\\": null, \\\"min_samples_start\\\": 100, \\\"n_covered_ex\\\": 10, \\\"binary_cache_size\\\": 10000, \\\"cache_margin\\\": 1000, \\\"verbose\\\": false, \\\"verbose_every\\\": 1, \\\"kwargs\\\": {}}, \\\"version\\\": \\\"0.9.1\\\"}, \\\"data\\\": {\\\"anchor\\\": [\\\"Marital Status = Never-Married\\\", \\\"Relationship = Own-child\\\"], \\\"precision\\\": 0.9705882352941176, \\\"coverage\\\": 0.0699, \\\"raw\\\": {\\\"feature\\\": [3, 5], \\\"mean\\\": [0.8094218415417559, 0.9705882352941176], \\\"precision\\\": [0.8094218415417559, 0.9705882352941176], \\\"coverage\\\": [0.3036, 0.0699], \\\"examples\\\": [{\\\"covered_true\\\": [[23, 4, 1, 1, 5, 1, 4, 0, 0, 0, 40, 9], [44, 4, 1, 1, 8, 0, 4, 1, 0, 0, 40, 9], [60, 2, 5, 1, 5, 1, 4, 0, 0, 0, 25, 9], [52, 4, 1, 1, 2, 0, 4, 1, 0, 0, 50, 9], [66, 6, 1, 1, 8, 0, 4, 1, 0, 0, 8, 9], [52, 4, 1, 1, 8, 0, 4, 1, 0, 0, 40, 9], [27, 4, 1, 1, 1, 1, 4, 1, 0, 0, 35, 9], [48, 4, 1, 1, 6, 0, 4, 1, 0, 0, 45, 9], [45, 6, 1, 1, 5, 0, 4, 1, 0, 0, 40, 9], [40, 2, 1, 1, 5, 4, 4, 0, 0, 0, 45, 9]], \\\"covered_false\\\": [[42, 6, 5, 1, 6, 0, 4, 1, 99999, 0, 80, 9], [29, 4, 1, 1, 8, 1, 4, 1, 0, 0, 50, 9], [49, 4, 1, 1, 8, 0, 4, 1, 0, 0, 50, 9], [34, 4, 5, 1, 8, 0, 4, 1, 0, 0, 40, 9], [38, 2, 1, 1, 5, 5, 4, 0, 7688, 0, 40, 9], [45, 7, 5, 1, 5, 0, 4, 1, 0, 0, 45, 9], [43, 4, 2, 1, 5, 0, 4, 1, 99999, 0, 55, 9], [47, 4, 5, 1, 6, 1, 4, 1, 27828, 0, 60, 9], [42, 6, 1, 1, 2, 0, 4, 1, 15024, 0, 60, 9], [56, 4, 1, 1, 6, 0, 2, 1, 7688, 0, 45, 9]], \\\"uncovered_true\\\": [], \\\"uncovered_false\\\": []}, {\\\"covered_true\\\": [[23, 4, 1, 1, 4, 3, 4, 1, 0, 0, 40, 9], [50, 2, 5, 1, 8, 3, 2, 1, 0, 0, 45, 9], [24, 4, 1, 1, 7, 3, 4, 0, 0, 0, 40, 3], [62, 4, 5, 1, 5, 3, 4, 1, 0, 0, 40, 9], [22, 4, 1, 1, 5, 3, 4, 1, 0, 0, 40, 9], [44, 4, 1, 1, 1, 3, 4, 0, 0, 0, 40, 9], [46, 4, 1, 1, 4, 3, 4, 1, 0, 0, 40, 9], [44, 4, 1, 1, 2, 3, 4, 1, 0, 0, 40, 9], [25, 4, 5, 1, 5, 3, 4, 1, 0, 0, 35, 9], [32, 2, 5, 1, 5, 3, 4, 1, 0, 0, 50, 9]], \\\"covered_false\\\": [[57, 5, 5, 1, 6, 3, 4, 1, 99999, 0, 40, 9], [44, 4, 1, 1, 8, 3, 4, 1, 7688, 0, 60, 9], [43, 2, 5, 1, 4, 3, 2, 0, 8614, 0, 47, 9], [56, 5, 2, 1, 5, 3, 4, 1, 99999, 0, 70, 9]], \\\"uncovered_true\\\": [], \\\"uncovered_false\\\": []}], \\\"all_precision\\\": 0, \\\"num_preds\\\": 1000000, \\\"success\\\": true, \\\"names\\\": [\\\"Marital Status = Never-Married\\\", \\\"Relationship = Own-child\\\"], \\\"prediction\\\": [0], \\\"instance\\\": [47.0, 4.0, 1.0, 1.0, 1.0, 3.0, 4.0, 1.0, 0.0, 0.0, 40.0, 9.0], \\\"instances\\\": [[47.0, 4.0, 1.0, 1.0, 1.0, 3.0, 4.0, 1.0, 0.0, 0.0, 40.0, 9.0]]}}}\"\r\n",
      "\t\t\t]\r\n",
      "\t\t}\r\n",
      "\t]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer income-explainer --inference-host ${MESH_IP}:80 \\\n",
    "     '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 12], \"datatype\": \"FP32\", \"data\": [[47,4,1,1,1,3,4,1,0,0,40,9]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "da7f239d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io \"income\" deleted\n",
      "model.mlops.seldon.io \"income-explainer\" deleted\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f ./models/income.yaml -n ${NAMESPACE}\n",
    "!kubectl delete -f ./models/income-explainer.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e694edee",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
